const $math = require('mathjs');

function comp(func, args) {
    try {
        let t = $math.chain($math.bignumber(args[0]));
        for (let i = 1; i < args.length; i += 1) {
            t = t[func]($math.bignumber(args[i]));
        }
        // 防止超过6位使用科学计数法
        return parseFloat(t.done());
    } catch (error) {
        window.console.log(error);
    }
}

const math = {
    add(...rest) {
        return comp('add', rest);
    },
    subtract(...rest) {
        return comp('subtract', rest);
    },
    multiply(...rest) {
        return comp('multiply', rest);
    },
    divide(...rest) {
        return comp('divide', rest);
    },
};

export default math;
